package com.aoxon.cargo.database;


import java.io.File;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Environment;
import android.telephony.TelephonyManager;

public class DatabaseHelper extends MySQLiteOpenHelper{
	private Context context;
	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		// TODO ������ݿ�󣬶���ݿ�Ĳ��� 
		db.execSQL(createClothTable);
		db.execSQL(createSupTable);
		db.execSQL(createStat);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		// TODO �����ݿ�汾�Ĳ���
		String deviceId ;
		try {
			TelephonyManager telMgr = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
			deviceId = telMgr.getDeviceId();
		} catch (Exception e) {
			e.printStackTrace();
			deviceId = "0";
		}
		
		String creatTable = "CREATE TABLE [new_stat] ("
			+"[_id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,"
			+"[fromId] VARCHAR(20) DEFAULT '0' NOT NULL,"
			+"[toId] VARCHAR(20) DEFAULT '0' NOT NULL,"
			+"[type] INTEGER DEFAULT '0' NOT NULL,"
			+"[time] VARCHAR(20) DEFAULT '0' NOT NULL,"
			+"[gps] VARCHAR(20) DEFAULT '0' NOT NULL"
			+")";
		
		String sql = "insert into new_stat (fromId , toId,type,time) " +
				"select fromId,toId,type,time from stat";
		
		String updateFromId = "update new_stat set fromId = ? where (1)";
		
		String deleteTable = "DROP TABLE stat";
		
		String changeTableName = "ALTER TABLE new_stat RENAME TO stat";
		db.execSQL(creatTable);
		db.execSQL(sql);
		db.execSQL(updateFromId, new String[]{deviceId});
		db.execSQL(deleteTable);
		db.execSQL(changeTableName);
		db.setVersion(newVersion);
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		super.onOpen(db);
		// TODO ÿ�γɹ�����ݿ�����ȱ�ִ�� 
	}
	
	@Override
	public File getDatabasePath(String name) {
		// TODO Auto-generated method stub
		String EXTERN_PATH = null;
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)==true)
		{
			EXTERN_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
			+ "/nahuo/db/";
			File f=new File(EXTERN_PATH);
			if(!f.exists())
			{
			  f.mkdirs();
			}
			return new File(EXTERN_PATH+ name);
		}
		return null;
	}

	private final String createClothTable = "CREATE TABLE [cloth] ("
			+"[clothId] INTEGER  NOT NULL PRIMARY KEY,"
			+"[type] INTEGER DEFAULT '1' NOT NULL,"
			+"[strJson] TEXT  NULL," +
			"[userId] INTEGER DEFAULT '0' NOT NULL)";
	private final String createSupTable = "CREATE TABLE [supplier] ("
			+"[supId] INTEGER  NOT NULL PRIMARY KEY,"
			+"[type] INTEGER DEFAULT '1' NOT NULL,"
			+"[strJson] TEXT  NULL," +
			"[userId] INTEGER DEFAULT '0' NOT NULL)";
	//创建统计表
	private final String createStat = "CREATE TABLE [stat] ("
			+"[_id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,"
			+"[fromId] INTEGER DEFAULT '0' NOT NULL,"
			+"[toId] INTEGER DEFAULT '0' NOT NULL,"
			+"[type] INTEGER DEFAULT '0' NOT NULL," 
			+"[time] VARCHAR(20) DEFAULT '0' NOT NULL,"
			+"[gps] VARCHAR(20) DEFAULT '0' NOT NULL"
			+")";

	}
